package com.hq.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.hq.bean.AmountByAge;
import com.hq.bean.AmountByGender;
import com.hq.bean.AmountByGenderAge;
import com.hq.bean.AmountByMarkAge;
import com.hq.service.AmountByProvinceService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class AmoutByAgeController {

    @Resource
    private AmountByProvinceService amountByProvinceService;


    @RequestMapping("/amountByAge")
    public Object amountByAge(){
        List<AmountByAge> amountByAges = amountByProvinceService.listByAmountAge();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");

        JSONArray jsonArray = new JSONArray();
        for (AmountByAge amountByAge : amountByAges) {
            /*
                        {
                  "name": "PC",
                  "value": 97,
                  "url": "http://www.baidu.com"
                },
             */
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("name",amountByAge.getAges());
            jsonObject1.put("value",amountByAge.getTotal());
            // 将创建好的json对象放到数组中
            jsonArray.add(jsonObject1);
        }
        jsonObject.put("data",jsonArray);
        return jsonObject;
    }

    // 按照性别
    @RequestMapping("/amountByGender")
    public Object amountByGender(){
        List<AmountByGender> amountByGender = amountByProvinceService.listByAmountGender();

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONArray jsonArray = new JSONArray();
        for (AmountByGender byGender : amountByGender) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("name",byGender.getGender());
            jsonObject1.put("value",byGender.getTotal());
            jsonArray.add(jsonObject1);
        }
        jsonObject.put("data",jsonArray);
        return jsonObject;
    }


    @RequestMapping("/amountByGenderAge")
    public Object amountByGenderAge(){
        List<AmountByGenderAge> amountByGenderAges = amountByProvinceService.listAmountByGenderAge();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject jsonObject1 = new JSONObject();
        JSONArray jsonArray1 = new JSONArray();
        //3个小的json对象
        JSONObject l1 = new JSONObject();
        l1.put("name","性别");
        l1.put("id","gender");
        JSONObject l2 = new JSONObject();
        l2.put("name","年龄");
        l2.put("id","age");
        JSONObject l3 = new JSONObject();
        l3.put("name","总金额");
        l3.put("id","total");
        jsonArray1.add(l1);
        jsonArray1.add(l2);
        jsonArray1.add(l3);
        jsonObject1.put("columns",jsonArray1);

        JSONArray jsonArray2 = new JSONArray();
        for (AmountByGenderAge amountByGenderAge : amountByGenderAges) {
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("gender",amountByGenderAge.getGender().equals("M")?"男":"女");
            jsonObject2.put("age",amountByGenderAge.getAges());
            jsonObject2.put("total",amountByGenderAge.getTotal());
            jsonArray2.add(jsonObject2);
        }
        jsonObject1.put("rows",jsonArray2);

        jsonObject.put("data",jsonObject1);

        return  jsonObject;
    }


    // 品牌年龄的指标分析
    @RequestMapping("/amountByMarkAge")
    public Object amountByMarkAge(){
        List<AmountByMarkAge> amountByMarkAges = amountByProvinceService.listAmountByMarkAge();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject jsonObject1 = new JSONObject();
        JSONArray jsonArray1 = new JSONArray();
        //3个小的json对象
        JSONObject l1 = new JSONObject();
        l1.put("name","品牌");
        l1.put("id","tm_name");
        JSONObject l2 = new JSONObject();
        l2.put("name","年龄");
        l2.put("id","ages");
        JSONObject l3 = new JSONObject();
        l3.put("name","总金额");
        l3.put("id","total");
        jsonArray1.add(l1);
        jsonArray1.add(l2);
        jsonArray1.add(l3);
        jsonObject1.put("columns",jsonArray1);

        JSONArray jsonArray2 = new JSONArray();
        for (AmountByMarkAge amountByMarkAge : amountByMarkAges) {
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("tm_name",amountByMarkAge.getTm_name());
            jsonObject2.put("ages",amountByMarkAge.getAges());
            jsonObject2.put("total",amountByMarkAge.getTotal());
            jsonArray2.add(jsonObject2);
        }
        jsonObject1.put("rows",jsonArray2);

        jsonObject.put("data",jsonObject1);

        return  jsonObject;
    }
}
